# ๐Ÿงก 02. ๋žŒ๋‹ค(lambda) | ํ•จ์ˆ˜ํ˜• ์ธํ„ฐํŽ˜์ด์Šค(Predicate, Consumer, Function)

# ๐Ÿ’Ž ๋žŒ๋‹ค (lambda)

๋žŒ๋‹ค ํ‘œํ˜„์‹์€ ๋ฉ”์†Œ๋“œ๋กœ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ์ต๋ช… ํ•จ์ˆ˜๋ฅผ ๋‹จ์ˆœํ™” ํ•œ ๊ฒƒ.

๋žŒ๋‹ค์˜ ํŠน์ง•์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

TIP

  1. ์ต๋ช…
    • ๋ณดํ†ต์˜ ๋ฉ”์†Œ๋“œ์™€ ๋‹ฌ๋ฆฌ ์ด๋ฆ„์ด ์—†์–ด์„œ ์ต๋ช…!
  2. ํ•จ์ˆ˜
    • ํŠน์ • ํด๋ž˜์Šค์— ์ข…์†๋˜์ง€ ์•Š์•„์„œ ํ•จ์ˆ˜๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.
  3. ์ „๋‹ฌ
    • ๋žŒ๋‹ค ํ‘œํ˜„์‹์„ ๋ฉ”์†Œ๋“œ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•˜๊ฑฐ๋‚˜ ๋ณ€์ˆ˜๋กœ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.
  4. ๊ฐ„๊ฒฐ์„ฑ
    • ์ต๋ช… ํด๋ž˜์Šค์ฒ˜๋Ÿผ ๋งŽ์€ ์ž์งˆ๊ตฌ๋ ˆํ•œ ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.

# ๐ŸŽ ์‚ฌ๊ณผ ํด๋ž˜์Šค


๋ฌด๊ฒŒ์™€ ์ƒ‰์ƒ ์ •๋ณด๊ฐ€ ๋‹ด๊ธด ์‚ฌ๊ณผ ํด๋ž˜์Šค ์ƒ์„ฑ

package javaStudy.mda02;

/**
 * ์‚ฌ๊ณผ
 */
public class Apple {
    public enum Color {
        RED,
        GREEN
    }

    Color color;
    Integer weight;

    public Apple(Color color, int weight) {
        this.color = color;
        this.weight = weight;
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }

    public Integer getWeight() {
        return weight;
    }

    public void setWeight(Integer weight) {
        this.weight = weight;
    }

    @Override
    public String toString() {
        return "Apple{" +
                "color=" + color +
                ", weight=" + weight +
                '}';
    }
}

# ๊ธฐ์กด ์ฝ”๋“œ


Comparator<Apple> byWeight = new Comparator<Apple>() {
    @Override
    public int compare(Apple o1, Apple o2) {
        return o1.getWeight().compareTo(o2.getWeight());
    }
};

# ๋žŒ๋‹ค ์ ์šฉ ์ฝ”๋“œ


Comparator<Apple> byWeight = (Apple o1, Apple o2) -> o1.getWeight().compareTo(o2.getWeight());

# ๐Ÿ’Ž ํ•จ์ˆ˜ํ˜• ์ธํ„ฐํŽ˜์ด์Šค

Predicate<T> ์™€ ๊ฐ™์€ ๊ฒŒ ํ•จ์ˆ˜ํ˜• ์ธํ„ฐํŽ˜์ด์Šค๋‹ค. ๋งŽ์€ ๋””ํดํŠธ ๋ฉ”์†Œ๋“œ(์ธํ„ฐํŽ˜์ด์Šค์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์€ ํด๋ž˜์Šค๋ฅผ ๊ณ ๋ คํ•ด์„œ ๊ธฐ๋ณธ ๊ตฌํ˜„์„ ์ œ๊ณตํ•˜๋Š” ๋ฐ”๋””๋ฅผ ํฌํ•จํ•˜๋Š” ๋ฉ”์†Œ๋“œ)๊ฐ€ ์žˆ๋”๋ผ๋„ ์ถ”์ƒ๋ฉ”์†Œ๋“œ๊ฐ€ ์˜ค์ง ํ•˜๋‚˜๋ฉด ํ•จ์ˆ˜ํ˜• ์ธํ„ฐํŽ˜์ด์Šค๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒƒ๋“ค์ด ์žˆ๋‹ค.

# Predicate


test ๋ผ๋Š” ์ถ”์ƒ ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•˜๋ฉฐ boolean์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

@FunctionalInterface
public interface Predicate<T> {
    boolean test(T t);
}
// ํ™œ์šฉ ์˜ˆ์ œ
package javaStudy.mda03;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

public class MainMethod {

    public static void main(String[] args) {
        List<String> listOfStrings = new ArrayList<>(Arrays.asList("apple","banana","melon", ""));

        Predicate<String> nonEmptyStringPredicate = (String s) -> !s.isEmpty();
        
        // [apple, banana, melon]
        List<String> nonEmpty = filter(listOfStrings, nonEmptyStringPredicate);
    }

    public static <T> List<T> filter(List<T> list, Predicate<T> p) {
        List<T> results = new ArrayList<>();
        for (T t : list) {
            if (p.test(t)) {
                results.add(t);
            }
        }
        return results;
    }
}

# Consumer


accept ๋ผ๋Š” ์ถ”์ƒ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•˜๋ฉฐ void๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

@FunctionalInterface
public interface Consumer<T> {
    void accept(T t);
}
// ํ™œ์šฉ ์˜ˆ์ œ
package javaStudy.mda03;

import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

public class ConsumerTest {
    public static void main(String[] args) {
        forEach(
            Arrays.asList(1,2,3,4,5),
                (Integer i) -> System.out.println(i)
        );
    }

    public static <T> void forEach(List<T> list, Consumer<T> c) {
        for(T t : list) {
            c.accept(t);
        }
    }
}

# Function


apply๋ผ๋Š” ์ถ”์ƒ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•˜๋ฉฐ, ์ œ๋„ค๋ฆญ ํ˜•์‹ T ๊ฐ์ฒด๋ฅผ ์ธ์ˆ˜๋กœ ๋ฐ›์•„ R ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

@FunctionalInterface
public interface Function<T, R> {
    R apply(T t);
}
// ํ™œ์šฉ
package javaStudy.mda03;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;

public class FunctionTest {
    public static void main(String[] args) {
        List<Integer> l = map(
                Arrays.asList("labmdas", "in", "action"),
                (String s) -> s.length()
        );
    }

    public static <T, R> List<R> map(List<T> list, Function<T, R> f) {
        List<R> result = new ArrayList<>();
        for(T t : list) {
            result.add(f.apply(t));
        }
        return result;
    }
}

# ๐Ÿ’Ž ๋ฉ”์†Œ๋“œ ์ฐธ์กฐ

# ๊ธฐ์กด์ฝ”๋“œ


appleList.sort((Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight()));

# ๋ฉ”์†Œ๋“œ ์ฐธ์กฐ


appleList.sort(Comparator.comparing(Apple::getWeight));
appleList.sort(Comparator.comparing(Apple::getWeight).reversed()); // ๋‚ด๋ฆผ์ฐจ์ˆœ
appleList.sort(Comparator.comparing(Apple::getWeight).reversed().thenComparing(Apple::getColor)); // ๋ฌด๊ฒŒ๊ฐ€ ๊ฐ™์œผ๋ฉด ์ƒ‰์ƒ๋ณ„๋กœ ์ •๋ ฌ

# ๐Ÿ’Ž Predicate ์กฐํ•ฉ

# apple List ์ƒ์„ฑ ๋ฐ filter ๋ฉ”์†Œ๋“œ ์ƒ์„ฑ


public static void main(String[] args) {
        List<Apple> appleList = Arrays.asList(
                new Apple(Apple.Color.RED, 180),
                new Apple(Apple.Color.GREEN, 100),
                new Apple(Apple.Color.RED, 100),
                new Apple(Apple.Color.GREEN, 350),
                new Apple(Apple.Color.RED, 400)
        );
}

public static <T> List<T> filter(List<T> list, java.util.function.Predicate<T> p) {
    List<T> results = new ArrayList<>();
    for (T t : list) {
        if (p.test(t)) {
            results.add(t);
        }
    }
    return results;
}

# ๊ธฐ์กด Predicate


// [Apple{color=GREEN, weight=100}, Apple{color=GREEN, weight=350}]
Predicate<Apple> greenApplePredicate = (apple) -> Apple.Color.GREEN.equals(apple.getColor());
filter(appleList, greenApplePredicate);

# negate()


๊ธฐ์กด Predicate ๋ฐ˜์ „

// [Apple{color=RED, weight=180}, Apple{color=RED, weight=100}, Apple{color=RED, weight=400}]
Predicate<Apple> notGreenApple = greenApplePredicate.negate();
filter(appleList, notGreenApple);

# and


Predicate ์—ฐ๊ฒฐ

// [Apple{color=GREEN, weight=350}]
Predicate<Apple> greenAndHeavyApple = greenApplePredicate.and(apple -> apple.getWeight() > 150);
filter(appleList, greenAndHeavyApple);

# or


Predicate ์ถ”๊ฐ€์กฐ๊ฑด

// [Apple{color=RED, weight=180}, Apple{color=RED, weight=100}, Apple{color=GREEN, weight=350}, Apple{color=RED, weight=400}]
Predicate<Apple> greenAndHeavyAppleOrRed = greenApplePredicate.and(apple -> apple.getWeight() > 150).or(apple -> Apple.Color.RED.equals(apple.getColor()));
filter(appleList, greenAndHeavyAppleOrRed);

# Reference


๋ชจ๋˜ ์ž๋ฐ” ์ธ ์•ก์…˜ (ํ•œ๋น›๋ฏธ๋””์–ด)

Last Updated: 3/8/2024, 5:46:31 AM